//
//  I_Account.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/4.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "I_Account.h"

@implementation I_Account

/**
 手机号加验证码登录
 */
+ (NetworkTask *)smsLoginWithMobile:(NSString *)mobile
                               code:(NSString *)code
                            success:(void(^)(M_Account *account))successBlock
                            failure:(ErrorBlock)failureBlock
{
    NSDictionary *params = @{@"mobile":SAFE_NIL_STRING(mobile),@"ciphertext":SAFE_NIL_STRING(code)};
    return [NET POST:@"/gym-coach-api/admin/1.0.0/smsLogin" parameters:params criticalValue:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull resultObject) {
        M_Account *account = [[M_Account alloc] initWithDictionary:SAFE_VALUE_FOR_KEY(resultObject, @"data") error:nil];
        account.loginWayType = LoginWayTypeByMobile;
        account.loginMobile = mobile;
        if (successBlock) {
            successBlock(account);
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}



/**
 账号登录
 */
+ (NetworkTask *)loginWithAccount:(NSString *)account
                              PWD:(NSString *)PWD
                          success:(void(^)(M_Account *account))successBlock
                          failure:(ErrorBlock)failureBlock
{
    NSDictionary *params = @{@"account":SAFE_NIL_STRING(account),
                             @"ciphertext":SAFE_NIL_STRING(PWD)
                             };
    return [NET POST:@"/gym-coach-api/admin/1.0.0/login" parameters:params criticalValue:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull resultObject) {
        M_Account *accountModel = [[M_Account alloc] initWithDictionary:SAFE_VALUE_FOR_KEY(resultObject, @"data") error:nil];
        accountModel.loginWayType = LoginWayTypeByAccount;
        accountModel.loginAccount = account;
        RI.account = accountModel;
        if (successBlock) {
            successBlock(accountModel);
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}


/**
 修改密码
 */
+ (NetworkTask *)modifyPWDWithOldPWD:(NSString *)oldPwd
                              newPwd:(NSString *)newPwd
                             success:(void(^)(void))successBlock
                             failure:(ErrorBlock)failureBlock
{
    NSDictionary *params = @{@"oldPwd":SAFE_NIL_STRING(oldPwd),
                             @"newPwd":SAFE_NIL_STRING(newPwd)
                             };
    
    return [NET POST:@"/gym-coach-api/admin/1.0.0/pwdUpdate" parameters:params criticalValue:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull resultObject) {
        if (successBlock) {
            successBlock();
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

/**
 微信登录
 
 @param accessToken 微信accessToken
 @param openId 微信openID
 @param unionid 微信unionId
 @param successBlock 成功回调
 @param failureBlock 失败回调
 
 */
+ (NetworkTask *)loginByWeixinWithAccessToken:(NSString *)accessToken
                                    andOpenId:(NSString *)openId
                                   andUnionid:(NSString *)unionid
                                      success:(void(^)(M_Account *account))successBlock
                                      failure:(ErrorBlock)failureBlock
{
    NSDictionary *params = @{@"accessToken":SAFE_NIL_STRING(accessToken),@"openId":SAFE_NIL_STRING(openId),@"unionid":SAFE_NIL_STRING(unionid)};
    
    return [NET POST:@"/gym-coach-api/auth/1.0.0/wechatLogin" parameters:params criticalValue:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull resultObject) {
        if (successBlock) {
            M_Account *account = [[M_Account alloc] initWithDictionary:SAFE_VALUE_FOR_KEY(resultObject, @"data") error:nil];
            account.loginWayType = LoginWayTypeByWeChat;
            successBlock(account);
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

/**
 获取账户过期时间
 */
+ (NetworkTask *)requestAccountEndDateOnSuccess:(void(^)(long long endDate))successBlock
                                        failure:(ErrorBlock)failureBlock
{
    return [NET POST:@"/gym-coach-api/admin/2.0.0/getAccountEndDate" parameters:nil criticalValue:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull resultObject) {
        if (successBlock) {
            successBlock([SAFE_VALUE_FOR_KEY(SAFE_VALUE_FOR_KEY(resultObject, @"data"), @"endDate") longLongValue]);
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}



@end
